<?php
/**
 * Created by PhpStorm.
 * User: changyifan
 * Date: 2018/3/9
 * Time: 下午4:53
 */

namespace Think\Event;


use Think\Controller\BaseController;

class BaseEvent {
    //获取学院列表
    public function getCollegeList(){
        $school = D('School','Model');
        //学院
        $college_info = $school -> getCollegeInfoListByAll();
        return $college_info;
    }
    //获取系部信息
    public function getDepatmentList(){
        $department = D('Department','Model');
        //系部
        $department_info = $department-> getDepartmentInfoListByAll();
        return $department_info;
    }
    //获取专业信息
    public function getProfessionList(){
        $profession = D('Profession','Model');
        //系部
        $profession_info = $profession-> getProfessionInfoListByAll();
        return $profession_info;
    }
    //获取班级信息
    public function getNaturalClassList(){
        $naturalclass= D('Naturalclass','Model');
        //班级
        $naturalclass_info = $naturalclass->getClassInfoListByAll();
        return $naturalclass_info;
    }
    //获取教师列表
    public function getTeacherList(){
        $teacher = D('Teacher','Model');
        //教师
        $teacher_info = $teacher->getTeacherInfoListByAll();
        return $teacher_info;
    }
    //获取课程信息列表
    public function getCourseList(){
        $course = D('Course','Model');
        //课程
        $course_info = $course->getCourseInfoListByAll();
        return $course_info;
    }
    //获取教学方案信息
    public function getTeacherMethodList(){
        $teachermethod = D('Teachingmethod','Model');
        //教学方案
        $teachermethod_info = $teachermethod -> getTeachingMethodInfoListByAll();
        return $teachermethod_info;
    }
    //获取章信息
    public function getChapterList(){
        $chapter = D('Chapter','Model');
        $chapter_info = $chapter->getChapterInfoListByAll();
        return $chapter_info;
    }
    //获取节信息
    public function getSectionList(){
        $section = D('Section','Model');
        $section_info = $section->getSectionInfoListByAll();
        return $section_info;
    }
    //获取知识点的信息（绑定于当前教师的课程相关）
    public function getPointInfoList(){
        $point = D('Point','Model');
        $point_info = $point -> getPointListByCourse();
        return $point_info;
    }
    //新增数据
    public function add_data($model,$data){
        if (!$model->create($data)){

            $result['state'] = false;
            $result['info'] = $model->getError().'1';

        }else{
            $info = $model -> add();
            if ($info){
                $result['state'] = true;
                $result['info'] = '新增成功';
            }else{
                $result['state'] = false;
                $result['info'] = $model->getError().'2';
            }
        }
        return $result;
    }
    //编辑数据
    public function edit_data($model,$data){
        if (!$model->create($data)){

            $result['state'] = false;
            $result['info'] = $model->getError();

        }else{
//            $condition['sch_id'] = $data[''];
            $info = $model ->save();
            if ($info){
                $result['state'] = true;
                $result['info'] = '编辑成功';
            }else{
                $result['state'] = false;
                $result['info'] = $model->getError();
            }
        }
        return $result;
    }
    //删除数据
    public function delete_data($model,$data){
        if (!$model->create($data)){

            $result['state'] = false;
            $result['info'] = $model->getError();

        }else{
            $info = $model->delete();
            if ($info){
                $result['state'] = true;
                $result['info'] = '删除成功';
            }else{
                $result['state'] = false;
                $result['info'] = $model->getError();
            }
        }
        return $result;
    }







    function import_excel($file){
        // 判断文件是什么格式
        $type = pathinfo($file);
        $type = strtolower($type["extension"]);
        if ($type=='xlsx') {
            $type='Excel2007';
        }elseif($type=='xls') {
            $type = 'Excel5';
        }
        ini_set('max_execution_time', '0');
//        Vendor('PHPExcel.PHPExcel.IOFactory');
        import("Org.PHPExcel","",".php");
        import("Org.PHPExcel.PHPExcel","",".php");
        // 判断使用哪种格式
        $objReader = \PHPExcel_IOFactory::createReader($type);
        $objPHPExcel = $objReader->load($file);
        $sheet = $objPHPExcel->getSheet(0);
        // 取得总行数
        $highestRow = $sheet->getHighestRow();
//        var_dump($highestRow);
        // 取得总列数
        $highestColumn = $sheet->getHighestColumn();
//        var_dump($highestColumn);
        //循环读取excel文件,读取一条,插入一条
        $data=array();
        //从第一行开始读取数据
        for($j=2;$j<=$highestRow;$j++){
            //从A列读取数据
            for($k='A';$k<=$highestColumn;$k++){
                // 读取单元格
                $data[$j][]=$objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue();
            }
        }
        return $data;
    }
    public function mapDataFromExcel($data){
        $info = array();
        foreach ($data as $key => $value){
            $info[$key]['export_student_id'] = $value[0];
            $info[$key]['export_student_name'] = $value[1];
            $info[$key]['export_student_sex'] = $value[2];
            $info[$key]['export_student_status'] = $value[3];
        }
        return $info;
    }
    /**
     * 快速导入第三方框架类库 所有第三方框架的类库文件统一放到 系统的Vendor目录下面
     * @param string $class 类库
     * @param string $baseUrl 基础目录
     * @param string $ext 类库后缀
     * @return boolean
     */
    function vendor($class, $baseUrl = '', $ext = '.php')
    {
        if (empty($baseUrl)) {
            $baseUrl = VENDOR_PATH;
        }

        return import($class, $baseUrl, $ext);
    }
    //删除视频
    public function deleteVideo($vid_id){
        $video = D('Video','Model');
        $filename = $video->getFileUUidById($vid_id);
        $filepath= VIDEO_PATH.$filename;
        var_dump($filepath);
        unlink($filepath);
    }
}